package core.views;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import core.Runner;

	
class MapPanel extends JPanel {
	  private static final long serialVersionUID = 1L;
	  private Image img;
	  private Line2D lin;
	  private Ellipse2D wagon;
	  private Graphics2D myG;
	  private Runner runner;
	  
//	  public static void main(String[] args) {
//		  MapPanel panel = new MapPanel(new ImageIcon("src/media/map.png").getImage());
//		
//		  JFrame frame = new JFrame();
//		  frame.getContentPane().add(panel);
//		  frame.pack();
//		  frame.setVisible(true);
//	  }

	  public MapPanel(Runner runner) {
		  this.runner = runner;
		  double xValue = 550 - ((((double)runner.getGroup().getDistance()) / 1500) * 490);
		  double yValue = 280 - ((((double)runner.getGroup().getDistance()) / 1500) * 190);
		  img = new ImageIcon("src/media/map.png").getImage();
		  lin = new Line2D.Float(60, 90, 550, 280);
		  wagon = new Ellipse2D.Double(xValue,yValue,10,10);
//	      this.img = img;
	      Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	      setPreferredSize(size);
	      setMinimumSize(size);
	      setMaximumSize(size);
	      setSize(size);
	      setLayout(null);
	  }
	  
	  public void updateMap()
	  {
		  double xValue = 550 - ((((double)runner.getGroup().getDistance()) / 1500) * 490);
		  double yValue = 280 - ((((double)runner.getGroup().getDistance()) / 1500) * 190);
		  img = new ImageIcon("src/media/map.png").getImage();
		  lin = new Line2D.Float(60, 90, 550, 280);
		  wagon = new Ellipse2D.Double(xValue,yValue,10,10);
//	      this.img = img;
	      Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	      setPreferredSize(size);
	      setMinimumSize(size);
	      setMaximumSize(size);
	      setSize(size);
	      setLayout(null);
	  }
	  public void paintComponent(Graphics g) {
		 myG = (Graphics2D) g;
		 drawMapAndLine();
		 myG.fill(wagon);
	  }
	  
	  public void drawMapAndLine() {
		  myG.setColor(Color.BLACK);
		  myG.drawImage(img, 0, 0, null);
	      myG.draw(lin);
	      myG.setColor(Color.RED);
	  }
	  
	  public void drawWagon(int x, int y) {
		  wagon = new Ellipse2D.Double(lin.getP2().getX()-7.5,lin.getP2().getY()-7.5,15,15);
		  myG.fill(wagon);
	      toString();
	  }	  
	  
	  public void update() {
		  drawMapAndLine();
		  
	  }
}
	